package usaco.c2009.march.bronze;

import java.util.Scanner;

/*
 * p3204
 * USACO 2009 March - Bronze
 * Hint: trivial
 */

public class p3204 {
	
	static int R, C, depth;
	static int[][] m;
	static int[] rm = {-1,-1,-1,0,0,1,1,1};
	static int[] cm = {-1,0,1,-1,1,-1,-0,1};

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		R = scanner.nextInt();
		C = scanner.nextInt();
		m = new int[R+1][C+1];
		for (int i=1;i<=R;++i) {
			for (int j=1;j<=C;++j) {
				m[i][j] = scanner.nextInt();
			}
		}
		
		depth = -1;
		for (int i=1;i<=R;++i) {
			for (int j=1;j<=C;++j) {
				boolean ok = false;
				for (int k=0;k<rm.length;++k) {
					int r = i + rm[k];
					int c = j + cm[k];
					if (r >= 1 && r <= R && c >= 1 && c <= C) {
						if (m[r][c] == m[i][j]) {
							ok = true;
						}
					}
					if (ok) {
						break;
					}
				}
				if (ok) {
					if (m[i][j] > depth) {
						depth = m[i][j];
					}
				}
			}
		}
		System.out.println(depth);

	}

}
